package cn.yuanqiao.archive.collection.appManager.service.impl;


import cn.yuanqiao.archive.collection.appManager.domain.YqDataCategory;
import cn.yuanqiao.archive.collection.appManager.domain.YqDataTree;
import cn.yuanqiao.archive.collection.appManager.mapper.YqDataCategoryMapper;
import cn.yuanqiao.archive.collection.appManager.mapper.YqDataTreeMapper;
import cn.yuanqiao.archive.collection.appManager.service.IYqDataCategoryService;
import cn.yuanqiao.archive.collection.danganjieshou.domain.YqGcReceiveFolder;
import cn.yuanqiao.archive.collection.danganjieshou.mapper.YqGcReceiveFolderMapper;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 资料树类别Service业务层处理
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.archive.appManager.service.impl
 * @ClassName: YqDataCategoryServiceImpl
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/7/28 14:25
 * @Version: 1.0
 */
@Service
public class YqDataCategoryServiceImpl implements IYqDataCategoryService {

    @Autowired
    private YqDataCategoryMapper yqDataCategoryMapper;

    @Autowired
    private YqDataTreeMapper yqDataTreeMapper;

    @Autowired
    private YqGcReceiveFolderMapper yqGcReceiveFolderMapper;

    public static final Logger log = LoggerFactory.getLogger(YqDataCategoryServiceImpl.class);

    /**
     * 查询资料树类别
     *
     * @param id 资料树类别主键
     * @return 资料树类别
     */
    @Override
    public YqDataCategory selectYqDataCategoryById(String id) {
        return yqDataCategoryMapper.selectYqDataCategoryById(id, new YqDataCategory().getTableName());
    }

    /**
     * 查询资料树类别列表
     *
     * @param treeCode 资料树Code
     * @return 资料树类别
     */
    @Override
    public List<YqDataCategory> selectYqDataCategoryList(String treeCode) {
        return yqDataCategoryMapper.selectYqDataCategoryByTreeCode(treeCode, new YqDataCategory().getTableName());
    }


    /**
     * 查询资料树类别列表
     *
     * @param yqDataCategory 资料树类别
     * @return 资料树类别
     */
    @Override
    public List<YqDataCategory> selectYqDataCategoryList(YqDataCategory yqDataCategory) {
        return yqDataCategoryMapper.selectYqDataCategoryList(yqDataCategory, new YqDataCategory().getTableName());
    }

    /**
     * 资料树类别导入处理
     *
     * @param yqDataCategory
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<YqDataCategory> yqDataCategory) {
        int rows = 0;
        List<String> failNum = new ArrayList<>();
        for (YqDataCategory data : yqDataCategory) {
            log.info("导入序号:{},数据:{}", data.getNum(), data.toString());
            if (StringUtils.isEmpty(data.getCategoryName())) {
                failNum.add("序号:" + data.getNum() + "类号名称不能为空");
                continue;
            }
            if (StringUtils.isEmpty(data.getCategoryCode())) {
                failNum.add("序号:" + data.getNum() + "类号代码不能为空");
                continue;
            }
            if (StringUtils.isEmpty(data.getOrderNum())) {
                failNum.add("序号:" + data.getNum() + "顺序不能为空");
                continue;
            }
            if (StringUtils.isEmpty(data.getControlType())) {
                failNum.add("序号:" + data.getNum() + "控制熟悉不能为空");
                continue;
            }
            if (StringUtils.isEmpty(data.getTreeCode())) {
                failNum.add("序号:" + data.getNum() + "资料树编码不能为空");
                continue;
            }
            List<YqDataTree> yqDataTrees = yqDataTreeMapper.selectYqDataTreeByCode(data.getTreeCode(), new YqDataTree().getTableName());
            if (yqDataTrees == null || yqDataTrees.size() < 1) {
                failNum.add("序号:" + data.getNum() + "资料树编码不存在" + data.getTreeCode());
                continue;
            }
            rows += insertYqDataCategory(data);
        }
        StringBuilder result = new StringBuilder("本次需要导入数量:" + yqDataCategory.size() + ",成功数量:" + rows);
        if (failNum.size() != 0) {
            result.append(",失败数量:" + failNum.size() + ",失败数据:" + failNum.toString());
        }
        return result.toString();
    }

    /**
     * 新增资料树类别
     *
     * @param yqDataCategory 资料树类别
     * @return 结果
     */
    @Override
    public int insertYqDataCategory(YqDataCategory yqDataCategory) {
        List<YqDataCategory> yqDataCategories = yqDataCategoryMapper.selectYqDataCategoryByCategoryCode(yqDataCategory.getCategoryCode(), new YqDataCategory().getTableName());
        if (yqDataCategories == null || yqDataCategories.size() > 0) {
            if (yqDataCategories.stream().anyMatch(a -> !a.getId().equals(yqDataCategory.getId()))) {
                throw new ServiceException("资料树类别代码重复，请重新填写！");
            }
        }
        yqDataCategory.setId(String.valueOf(SnowflakeIdGenerator.generateId()));
        yqDataCategory.setCreateTime(DateUtils.getNowDate());
        yqDataCategory.setCreator(SecurityUtils.getUsername());
        yqDataCategory.setCreatorId(String.valueOf(SecurityUtils.getUserId()));
        return yqDataCategoryMapper.insertYqDataCategory(yqDataCategory, new YqDataCategory().getTableName());
    }

    /**
     * 修改资料树类别
     *
     * @param yqDataCategory 资料树类别
     * @return 结果
     */
    @Override
    public int updateYqDataCategory(YqDataCategory yqDataCategory) {
        List<YqDataCategory> yqDataCategories = yqDataCategoryMapper.selectYqDataCategoryByCategoryCode(yqDataCategory.getCategoryCode(), new YqDataCategory().getTableName());
        if (yqDataCategories != null && yqDataCategories.size() > 0) {
            if (yqDataCategories.stream().anyMatch(a -> !a.getId().equals(yqDataCategory.getId()))) {
                throw new ServiceException("资料树类别代码重复，请重新填写！");
            }
        }
        return yqDataCategoryMapper.updateYqDataCategory(yqDataCategory, new YqDataCategory().getTableName());
    }

    /**
     * 批量删除资料树类别
     *
     * @param ids 需要删除的资料树类别主键
     * @return 结果
     */
    @Override
    public int deleteYqDataCategoryByIds(String[] ids) {
        for (String id : ids) {
            //查询资料类别信息
            YqDataCategory yqDataCategory = yqDataCategoryMapper.selectYqDataCategoryById(id, new YqDataCategory().getTableName());
            List<YqGcReceiveFolder> folders = yqGcReceiveFolderMapper.selectCategoryNum(yqDataCategory.getCategoryCode(), new YqGcReceiveFolder().getTableName());
            if (folders != null && folders.size() > 0) {
                throw new ServiceException("请先删除该资料类别下的文件!!");
            }
        }
        return yqDataCategoryMapper.deleteYqDataCategoryByIds(ids, new YqDataCategory().getTableName());
    }

    /**
     * 删除资料树类别信息
     *
     * @param id 资料树类别主键
     * @return 结果
     */
    @Override
    public int deleteYqDataCategoryById(String id) {
        return yqDataCategoryMapper.deleteYqDataCategoryById(id, new YqDataCategory().getTableName());
    }
}
